<template>
  <div class="register-fullscreen" id="register-page">
    <!-- 添加标题 -->
    <h1 class="site-title">有铭汽车</h1>
    
    <!-- 添加左侧文本内容 -->
    <div class="left-content">
      <div class="tagline-large">用汽车</div>
      <div class="tagline-small">让复杂的行程更轻松</div>
    </div>
    
    <div class="register-container">
      <div class="register-header">
        <h2>欢迎注册</h2>
      </div>
      <div class="login-prompt">
        <span>已有账号？</span>
        <a @click="goToLogin">登录</a>
      </div>
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label for="username">
            账号
            <div class="label-tooltip">
              <img 
                src="/images/tip.png" 
                alt="提示" 
                class="tooltip-icon"
                @mouseenter="showUsernameTooltip = true"
                @mouseleave="showUsernameTooltip = false"
              />
              <div v-show="showUsernameTooltip" class="tooltip" :class="{ 'show': showUsernameTooltip }">
                <div class="tooltip-content">
                  <p>账号格式要求：</p>
                  <ul>
                    <li>长度为3-12个字符</li>
                    <li>只能包含英文字母、数字和下划线</li>
                  </ul>
                </div>
              </div>
            </div>
          </label>
          <input 
            type="text" 
            id="username" 
            v-model="registerForm.username" 
            :class="{ 'error': errors.username }"
            placeholder="请输入账号"
            autocomplete="username"
          />
          <div v-if="errors.username" class="error-message">{{ errors.username }}</div>
        </div>
        <div class="form-group">
          <label for="password">
            密码
            <div class="label-tooltip">
              <img 
                src="/images/tip.png" 
                alt="提示" 
                class="tooltip-icon"
                @mouseenter="showPasswordTooltip = true"
                @mouseleave="showPasswordTooltip = false"
              />
              <div v-show="showPasswordTooltip" class="tooltip" :class="{ 'show': showPasswordTooltip }">
                <div class="tooltip-content">
                  <p>密码格式要求：</p>
                  <ul>
                    <li>长度为6-18个字符</li>
                    <li>只能包含英文字母和数字</li>
                  </ul>
                </div>
              </div>
            </div>
          </label>
          <input 
            type="password" 
            id="password" 
            v-model="registerForm.password" 
            :class="{ 'error': errors.password }"
            placeholder="请输入密码"
            autocomplete="new-password"
          />
          <div v-if="errors.password" class="error-message">{{ errors.password }}</div>
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input 
            type="password" 
            id="confirmPassword" 
            v-model="registerForm.confirmPassword" 
            :class="{ 'error': errors.confirmPassword }"
            placeholder="请再次输入密码"
            autocomplete="new-password"
          />
          <div v-if="errors.confirmPassword" class="error-message">{{ errors.confirmPassword }}</div>
        </div>
        <!-- 添加用户协议同意区域 -->
        <div class="agreement-section">
          <label class="agreement-checkbox">
            <input 
              type="checkbox" 
              v-model="registerForm.agreeToTerms"
            />
            <span class="checkmark"></span>
            <span class="agreement-text">
              阅读并接受
              <a href="#" class="agreement-link">《用户协议》</a>、
              <a href="#" class="agreement-link">《个人信息保护声明》</a>
              及
              <a href="#" class="agreement-link">《隐私权保护声明》</a>
            </span>
          </label>
        </div>
        <button 
          type="submit" 
          class="register-btn"
          :disabled="!registerForm.agreeToTerms"
        >
          注册
        </button>
      </form>
    </div>
    <!-- 添加版权信息 -->
    <div class="copyright">
      <a href="#" class="help-link">帮助中心</a> | 
      © 2025 有铭汽车
    </div>

    <!-- 注册成功确认对话框 -->
    <ConfirmDialog
      v-model:visible="showSuccessDialog"
      title="注册成功"
      message="注册成功，去登录吧"
      confirm-text="确定"
      cancel-text=""
      @confirm="handleSuccessConfirm"
    />
  </div>
</template>

<script>
import { authAPI } from '../api/auth.js'
import ConfirmDialog from '../components/ConfirmDialog.vue'

export default {
  name: 'RegisterView',
  components: {
    ConfirmDialog
  },
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        agreeToTerms: false
      },
      errors: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      isLoading: false,
      showSuccessDialog: false,
      showUsernameTooltip: false,
      showPasswordTooltip: false
    }
  },
  methods: {
    validateForm() {
      // 清除之前的错误信息
      this.errors = {
        username: '',
        password: '',
        confirmPassword: ''
      };
      
      let isValid = true;
      
      // 验证账号
      if (!this.registerForm.username) {
        this.errors.username = '账号不能为空';
        isValid = false;
      } else if (this.registerForm.username.length < 3 || this.registerForm.username.length > 12) {
        this.errors.username = '账号长度必须为3-12个字符';
        isValid = false;
      } else if (!/^[a-zA-Z0-9_]+$/.test(this.registerForm.username)) {
        this.errors.username = '账号只能包含英文字母、数字和下划线';
        isValid = false;
      }
      
      // 验证密码
      if (!this.registerForm.password) {
        this.errors.password = '密码不能为空';
        isValid = false;
      } else if (this.registerForm.password.length < 6 || this.registerForm.password.length > 18) {
        this.errors.password = '密码长度必须为6-18个字符';
        isValid = false;
      } else if (!/^[a-zA-Z0-9]+$/.test(this.registerForm.password)) {
        this.errors.password = '密码只能包含英文字母和数字';
        isValid = false;
      }
      
      // 验证确认密码
      if (!this.registerForm.confirmPassword) {
        this.errors.confirmPassword = '确认密码不能为空';
        isValid = false;
      } else if (this.registerForm.confirmPassword.length < 6 || this.registerForm.confirmPassword.length > 18) {
        this.errors.confirmPassword = '密码长度必须为6-18个字符';
        isValid = false;
      } else if (!/^[a-zA-Z0-9]+$/.test(this.registerForm.confirmPassword)) {
        this.errors.confirmPassword = '密码只能包含英文字母和数字';
        isValid = false;
      }
      
      // 验证两次密码是否一致
      if (this.registerForm.password && this.registerForm.confirmPassword && 
          this.registerForm.password !== this.registerForm.confirmPassword) {
        this.errors.confirmPassword = '两次输入的密码不一致';
        isValid = false;
      }
      
      // 验证是否同意协议
      if (!this.registerForm.agreeToTerms) {
        // 这里我们不设置错误信息，而是通过按钮禁用来提示用户
        // 但为了保持表单验证的一致性，我们也可以添加一个错误提示
        // 不过根据需求，按钮禁用应该已经足够提示用户了
      }
      
      return isValid;
    },
    
    async handleRegister() {
      // 验证表单
      if (!this.validateForm()) {
        return;
      }
      
      this.isLoading = true;
      
      try {
        // 调用后端注册接口
        const response = await this.registerUser();
        
        if (response.success) {
          // 显示成功对话框
          this.showSuccessDialog = true;
        } else {
          // 处理注册失败
          if (response.message.includes('用户名已存在')) {
            this.errors.username = '该用户名已被注册，请选择其他用户名';
          } else {
            alert(response.message || '注册失败');
          }
        }
      } catch (error) {
        console.error('注册错误:', error);
        alert('注册请求失败，请检查网络连接');
      } finally {
        this.isLoading = false;
      }
    },
    
    async registerUser() {
      try {
        const response = await fetch('http://localhost:5000/api/register', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            username: this.registerForm.username,
            password: this.registerForm.password,
            confirmPassword: this.registerForm.confirmPassword
          })
        });
        
        const result = await response.json();
        return result;
      } catch (error) {
        console.error('注册请求失败:', error);
        return {
          success: false,
          message: '网络连接失败，请检查后端服务器是否运行'
        };
      }
    },
    
    handleSuccessConfirm() {
      // 跳转到登录页面
      this.$router.push('/login');
    },
    goToLogin() {
      this.$router.push('/login');
    }
  }
}
</script>

<style scoped>
.register-fullscreen {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  width: 100%;
  height: 100%;
  background-image: url('/images/register.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 20px;
  position: relative;
  z-index: 1;
}

/* 添加标题样式 */
.site-title {
  position: fixed;
  top: 40px;
  left: 40px;
  font-size: 2rem;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  z-index: 2;
  margin: 0;
  font-weight: 600;
}

/* 添加左侧文本内容样式 */
.left-content {
  position: fixed;
  top: 40%;
  left: 170px;
  transform: translateY(-50%);
  z-index: 2;
  text-align: left;
}

.tagline-large {
  font-size:4rem;
  color: #FFFFFF;
  font-weight: bold;
  margin-bottom: 2px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.tagline-small {
  font-size: 2.5rem;
  color: #FFFFFF;
  font-weight: normal;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}

/* 添加logo样式 */
.logo-container {
  position: fixed;
  top: 20px;
  left: 20px;
  z-index: 2;
}

.logo {
  height: 40px;
  width: auto;
}

.register-container {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);
  border-radius: 15px; /* 增加边框圆角弧度 */
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  padding: 2.5rem 2rem;
  position: relative;
  overflow: hidden;
  margin-right: 180px;
}

.register-header {
  text-align: center;
  margin-bottom: 0.8rem;
}

.register-header h2 {
  color: #222;
  font-size: 1.8rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  letter-spacing: 0.5px;
}

.login-prompt {
  text-align: center;
  margin-bottom: 0.8rem;
  color: #666;
  font-size: 0.95rem;
}

.login-prompt a {
  color: #4CAF50;
  cursor: pointer;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.2s ease;
  position: relative;
  margin-left: 0.3rem;
}

.login-prompt a:hover {
  color: #66BB6A;
  text-decoration: none;
}

.login-prompt a::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 1px;
  background-color: #4CAF50;
  transition: width 0.3s ease;
}

.login-prompt a:hover::after {
  width: 100%;
}

.register-form {
  margin-bottom: 1.5rem;
}

.form-group {
  margin-bottom: 1.4rem;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 0.6rem;
  color: #444;
  font-weight: 500;
  font-size: 0.95rem;
  position: relative;
  display: flex;
  align-items: center;
}

/* 移除必填字段的红色星号 */
/* .form-group label.required::after {
  content: " *";
  color: #f5222d;
  font-weight: bold;
} */

/* 添加提示图标和提示框样式 */
.label-tooltip {
  display: inline-flex;
  align-items: center;
  margin-left: 8px;
  position: relative;
  cursor: pointer;
}

.tooltip-icon {
  width: 16px;
  height: 16px;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.2s ease;
}

.tooltip-icon:hover {
  opacity: 1;
}

.tooltip {
  position: absolute;
  top: 100%;
  left: 0;
  background: #333;
  color: white;
  border-radius: 6px;
  padding: 10px 14px;
  font-size: 0.85rem;
  width: 220px;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  margin-top: 8px;
  transform: translateY(0);
  transition: transform 0.2s ease, opacity 0.2s ease;
  opacity: 0;
  pointer-events: none;
}

.tooltip.show {
  transform: translateY(5px);
  opacity: 1;
}

.tooltip::before {
  content: '';
  position: absolute;
  top: -5px;
  left: 15px;
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #333;
}

.tooltip-content p {
  margin: 0 0 5px 0;
  font-weight: bold;
}

.tooltip-content ul {
  margin: 0;
  padding-left: 15px;
}

.tooltip-content li {
  margin: 3px 0;
}

.form-group input {
  width: 100%;
  padding: 0.9rem 1.1rem;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 1rem;
  transition: all 0.2s ease;
  box-sizing: border-box;
  background-color: #fff;
  font-weight: 400;
}

.form-group input:focus {
  outline: none;
  border-color: #4CAF50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
  background-color: #fff;
}

.form-group input:hover:not(:focus) {
  border-color: #bfbfbf;
}

.form-group input.error {
  border-color: #f5222d;
  box-shadow: 0 0 0 2px rgba(245, 34, 45, 0.15);
}

.form-group input::placeholder {
  color: #bfbfbf;
  font-size: 0.95rem;
}

.error-message {
  color: #f5222d;
  font-size: 0.85rem;
  margin-top: 0.4rem;
  display: flex;
  align-items: center;
  padding-left: 0.1rem;
}

.error-message::before {
  content: "!";
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background-color: #f5222d;
  color: white;
  font-size: 9px;
  font-weight: bold;
  text-align: center;
  line-height: 14px;
  margin-right: 6px;
  flex-shrink: 0;
}

/* 添加协议同意区域样式 */
.agreement-section {
  margin-bottom: 1.5rem;
}

.agreement-checkbox {
  display: flex;
  align-items: flex-start;
  font-size: 0.9rem;
  color: #666;
  cursor: pointer;
  line-height: 1.5;
}

.agreement-checkbox input[type="checkbox"] {
  display: none;
}

.agreement-checkbox .checkmark {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 1px solid #d9d9d9;
  border-radius: 3px;
  margin-right: 8px;
  position: relative;
  flex-shrink: 0;
  margin-top: 2px;
}

.agreement-checkbox input[type="checkbox"]:checked + .checkmark::after {
  content: "";
  position: absolute;
  left: 5px;
  top: 1px;
  width: 4px;
  height: 8px;
  border: solid #338FFF;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.agreement-text {
  display: inline-block;
  line-height: 1.5;
}

.agreement-link {
  color: #4CAF50;
  text-decoration: none;
  white-space: nowrap;
}

.agreement-link:hover {
  color: #66BB6A;
  text-decoration: underline;
}

.register-btn {
  width: 100%;
  padding: 0.95rem;
  background: linear-gradient(135deg, #4CAF50 0%, #66BB6A 100%);
  color: white;
  border: none;
  border-radius: 25px; /* 使按钮左右呈现半圆弧效果 */
  font-size: 1.05rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  letter-spacing: 1px;
  box-shadow: 0 2px 8px rgba(76, 175, 80, 0.25);
  margin-top: 0.5rem;
}

.register-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #66BB6A 0%, #4CAF50 100%);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.35);
  transform: translateY(-1px);
}

.register-btn:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 0 2px 6px rgba(76, 175, 80, 0.25);
}

.register-btn:disabled {
  background: #cccccc;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.copyright {
  position: fixed;
  left: 70px;
  bottom: 70px;
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.9rem;
  z-index: 2;
}

.help-link {
  color: rgba(255, 255, 255, 0.6);
  text-decoration: none;
}

.help-link:hover {
  color: white;
}

#register-page {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('/images/register.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
</style>